home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / CModalProgress 1.1 / CTestDialogs.cp < prev    next >
Text File  |  1995-02-18  |  9KB  |  374 lines

  1. // ===========================================================================
  2. //    CTestDialogs.cp
  3. //
  4. //    This file contains the definition information for the example code to
  5. //    demonstrate the functionality of the CModalProgress class.
  6. //
  7. //    NOTE:  This is just an example of how to use the CModalProgress class. Use
  8. //           this code as a base for your own specific uses.
  9. //
  10. //    Copyright 1994, 1995 Alysoft Solutions. All rights reserved.
  11. //
  12. // ===========================================================================
  13.  
  14. #include "CTestDialogs.h"
  15.  
  16. /*
  17.  *  TEST 1
  18.  *    This test dialog simply shows a piece of text, a standard one state progress bar and a 
  19.  *    stop, or abort button.
  20.  */
  21.  
  22. void TestDialog1()
  23. {
  24.     CModalProgress            *dlg ;
  25.     long                    total = 0 ;
  26.     short                    progress = kDialogContinues ;
  27.     long                    delay ;
  28.     short                    stateSpace ;
  29.     
  30.     dlg = new CModalProgress ;
  31.     if (dlg->SetupDialog(kTest1DialogResID) != noErr)
  32.     {
  33.         SysBeep(1) ;
  34.         return ;
  35.     }
  36.     dlg->SetProgressBar(kTest1ProgressBarItem) ;
  37.  
  38.     dlg->SetCurrentState(100) ;
  39.     dlg->SetStateSpace(300) ;
  40.     dlg->BeginModal() ;
  41.  
  42.     while (progress == kDialogContinues)
  43.     {
  44.         progress = dlg->ProcessModal() ;
  45.         Delay(1, &delay) ;
  46.         stateSpace = dlg->SetCurrentStateValue(total++) ;
  47.     }
  48.     
  49.     dlg->EndModal() ;
  50.     delete dlg ;
  51.  
  52. }
  53.  
  54. /*
  55.  *    TEST 2
  56.  *    This test shows a more complex scenario, there is some changing text in the dialog which
  57.  *    is modified at state change time, there is a progress bar and there are multiple state
  58.  *    spaces.
  59.  */
  60.     
  61. void CDialog2::SetCurrentState(float statePercent)
  62. {
  63.     short                    itemType ;
  64.     Handle                    theItem ;
  65.     Rect                    box ;
  66.     
  67.     GetDialogItem(fDialog, kTest2ItemText, &itemType, &theItem, &box) ;
  68.     
  69.     CModalProgress::SetCurrentState(statePercent) ;
  70.  
  71.     if (statePercent == kTest2State1)
  72.     {
  73.         SetIText(theItem, kTest2State1String) ;
  74.         SetStateSpace(kTest2State1StateSpace) ;
  75.     }
  76.         
  77.     if (statePercent == kTest2State2)
  78.     {
  79.         SetIText(theItem, kTest2State2String) ;
  80.         SetStateSpace(kTest2State2StateSpace) ;
  81.     }
  82.     
  83.     if (statePercent == kTest2State3)
  84.     {
  85.         SetIText(theItem, kTest2State3String) ;
  86.         SetStateSpace(kTest2State3StateSpace) ;
  87.     }
  88. }
  89.  
  90. void TestDialog2()
  91. {
  92.     CDialog2                *dlg ;
  93.     long                    total = 0 ;
  94.     short                    progress = kDialogContinues ;
  95.     long                    delay ;
  96.     short                    stateSpace = kStateSpaceWithinLimit ;
  97.     
  98.     dlg = new CDialog2 ;
  99.     if (dlg->SetupDialog(kTest2DialogResID) != noErr)
  100.     {
  101.         SysBeep(1) ;
  102.         return ;
  103.     }
  104.     dlg->SetProgressBar(kTest2ProgressBarItem) ;
  105.  
  106.     dlg->SetCurrentState(kTest2State1) ;
  107.     dlg->BeginModal() ;
  108.  
  109.     while ((progress == kDialogContinues) && (stateSpace == kStateSpaceWithinLimit))
  110.     {
  111.         progress = dlg->ProcessModal() ;
  112.         Delay(1, &delay) ;
  113.         stateSpace = dlg->SetCurrentStateValue(total++) ;
  114.     }
  115.     
  116.     if (progress == kDialogContinues)
  117.     {
  118.         dlg->SetCurrentState(kTest2State2) ;
  119.         total = 0 ;
  120.         stateSpace = kStateSpaceWithinLimit ;
  121.         while ((progress == kDialogContinues) && (stateSpace == kStateSpaceWithinLimit))
  122.         {
  123.             progress = dlg->ProcessModal() ;
  124.             Delay(1, &delay) ;
  125.             stateSpace = dlg->SetCurrentStateValue(total++) ;
  126.         }
  127.         
  128.         if (progress == kDialogContinues)
  129.         {
  130.             dlg->SetCurrentState(kTest2State3) ;
  131.             total = 0 ;
  132.             while (progress == kDialogContinues)
  133.             {
  134.                 progress = dlg->ProcessModal() ;
  135.                 Delay(1, &delay) ;
  136.                 stateSpace = dlg->SetCurrentStateValue(total++) ;
  137.             }
  138.         }
  139.     }
  140.     
  141.     dlg->EndModal() ;
  142.     delete dlg ;
  143.  
  144. }
  145.  
  146. /*
  147.  *  TEST 3
  148.  *    A simple search type dialog with the infinite progress bar.
  149.  *
  150.  *  This test also makes use of the alternative event handling method. Note the
  151.  *    use of the CanProcessEvent() and the ProcessIdle() method calls.
  152.  */
  153.  
  154. void TestDialog3()
  155. {
  156.     CModalProgress            *dlg ;
  157.     short                    progress = kDialogContinues ;
  158.     
  159.     HiliteMenu(0) ;
  160.     
  161.     dlg = new CModalProgress ;
  162.     if (dlg->SetupDialog(kTest3DialogResID) != noErr)
  163.     {
  164.         SysBeep(1) ;
  165.         return ;
  166.     }
  167.     dlg->SetInfiniteBar(kTest3InfiniteBarItem) ;
  168.  
  169.     dlg->BeginModal() ;
  170.  
  171.     while (progress == kDialogContinues)
  172.     {
  173.         EventRecord theEvent ;
  174.         
  175.         if (GetNextEvent(everyEvent, &theEvent))
  176.         {
  177.             if (!dlg->CanProcessEvent(&theEvent, &progress))
  178.                 switch (theEvent.what)
  179.                 {
  180.                     case inMenuBar:
  181.                         MenuSelect(theEvent.where) ;
  182.                         HiliteMenu(0) ;
  183.                         break ;
  184.                 }
  185.         }
  186.         else
  187.             progress = dlg->ProcessIdle() ;
  188.     }
  189.     
  190.     dlg->EndModal() ;
  191.     delete dlg ;
  192. }
  193.  
  194. /*
  195.  *    TEST 4
  196.  *    This is the most detailed of all the test. It comprises of a dialog which depicts a 
  197.  *    situation where the user is connecting to a remote server, downloading several files,
  198.  *    setting some parameter data on the server and then disconnecting from the server.
  199.  *    The dialog shows fields for the textual current state, the files being downloaded and
  200.  *    a progress bar to show time left.
  201.  */
  202.  
  203. void CDialog4::SetCurrentState(float statePercent)
  204. {
  205.     short                    itemType ;
  206.     Handle                    theItem ;
  207.     Rect                    box ;
  208.     
  209.     GetDialogItem(fDialog, kTest4StateTextItem, &itemType, &theItem, &box) ;
  210.     
  211.     CModalProgress::SetCurrentState(statePercent) ;
  212.  
  213.     switch ((long)statePercent)
  214.     {
  215.         case kTest4ConnectState:
  216.             SetIText(theItem, kTest4ConnectStateString) ;
  217.             SetStateSpace(kTest4StandardStateSpace) ;
  218.             break ;
  219.             
  220.         case kTest4LoginState:
  221.             SetIText(theItem, kTest4LoginStateString) ;
  222.             SetStateSpace(kTest4StandardStateSpace) ;
  223.             break ;
  224.             
  225.         case kTest4DownloadState:
  226.             SetIText(theItem, kTest4DownloadStateString) ;
  227.             SetStateSpace(kTest4DownloadStateSpace) ;
  228.             break ;
  229.             
  230.         case kTest4SetParamsState:
  231.             SetIText(theItem, kTest4SetParamsStateString) ;
  232.             SetStateSpace(kTest4StandardStateSpace) ;
  233.             break ;
  234.             
  235.         case kTest4LogoffState:
  236.             SetIText(theItem, kTest4LogoffStateString) ;
  237.             SetStateSpace(kTest4StandardStateSpace) ;
  238.             break ;
  239.             
  240.         case kTest4DisconnectState:
  241.             SetIText(theItem, kTest4DisconnectStateString) ;
  242.             SetStateSpace(kTest4StandardStateSpace) ;
  243.             break ;
  244.             
  245.         case kTest4CleanupState:
  246.             SetIText(theItem, kTest4CleanupStateString) ;
  247.             SetStateSpace(kTest4StandardStateSpace) ;
  248.             break ;
  249.             
  250.     }
  251. }
  252.  
  253. void CDialog4::SetFileName(Str255 filename)
  254. {
  255.     short                    itemType ;
  256.     Handle                    theItem ;
  257.     Rect                    box ;
  258.     
  259.     GetDialogItem(fDialog, kTest4FileNameTextItem, &itemType, &theItem, &box) ;
  260.     SetIText(theItem, filename) ;
  261. }
  262.  
  263. short PerformAState(CDialog4 *dlg, short whichState)
  264. {
  265.     short                    progress = kDialogContinues ;
  266.     short                    stateSpace = kStateSpaceWithinLimit ;
  267.     long                    total = 0 ;
  268.     
  269.     dlg->SetCurrentState(whichState) ;
  270.  
  271.     while ((stateSpace == kStateSpaceWithinLimit) && (progress == kDialogContinues))
  272.     {
  273.         progress = dlg->ProcessModal() ;
  274.         stateSpace = dlg->SetCurrentStateValue(total++) ;
  275.     }
  276.  
  277.     return progress ;
  278. }
  279.  
  280. void TestDialog4()
  281. {
  282.     CDialog4                *dlg ;
  283.     short                    progress = kDialogContinues ;
  284.     long                    total ;
  285.     
  286.     dlg = new CDialog4 ;
  287.     if (dlg->SetupDialog(kTest4DialogResID) != noErr)
  288.     {
  289.         SysBeep(1) ;
  290.         return ;
  291.     }
  292.     
  293.     dlg->SetProgressBar(kTest4ProgressBarItem) ;
  294.     dlg->SetPercentText(kTest4PercentTextItem, 5) ;
  295.     
  296.     dlg->SetFileName(kTest4DownloadFileNone) ;
  297.     
  298.     dlg->BeginModal() ;
  299.     
  300.     /* Process the connect state */
  301.     
  302.     if (PerformAState(dlg, kTest4ConnectState) != kDialogContinues)
  303.         goto ALL_DONE ;
  304.     
  305.     /* Process the login state */
  306.     
  307.     if (PerformAState(dlg, kTest4LoginState) != kDialogContinues)
  308.         goto ALL_DONE ;
  309.     
  310.     /* Perform the file downloads */
  311.     
  312.     dlg->SetCurrentState(kTest4DownloadState) ;
  313.     dlg->SetFileName(kTest4DownloadFile1) ;
  314.     
  315.     total = 0 ;
  316.     while ((progress == kDialogContinues) && (total < kTest4File1StateSpace))
  317.     {
  318.         progress = dlg->ProcessModal() ;
  319.         dlg->SetCurrentStateValue(total++) ;
  320.     }
  321.     if (progress != kDialogContinues)
  322.         goto ALL_DONE ;
  323.     
  324.     /* File 2 */
  325.     
  326.     dlg->SetFileName(kTest4DownloadFile2) ;
  327.     
  328.     while ((progress == kDialogContinues) && (total < (kTest4File2StateSpace + kTest4File1StateSpace)))
  329.     {
  330.         progress = dlg->ProcessModal() ;
  331.         dlg->SetCurrentStateValue(total++) ;
  332.     }
  333.     if (progress != kDialogContinues)
  334.         goto ALL_DONE ;
  335.  
  336.     /* File 3 */
  337.     
  338.     dlg->SetFileName(kTest4DownloadFile3) ;
  339.     
  340.     while ((progress == kDialogContinues) && (total < (kTest4File2StateSpace + kTest4File1StateSpace + kTest4File3StateSpace)))
  341.     {
  342.         progress = dlg->ProcessModal() ;
  343.         dlg->SetCurrentStateValue(total++) ;
  344.     }
  345.     if (progress != kDialogContinues)
  346.         goto ALL_DONE ;
  347.  
  348.     dlg->SetFileName(kTest4DownloadFileNone) ;
  349.  
  350.     /* Perfrom the Parameter setting */
  351.  
  352.     if (PerformAState(dlg, kTest4SetParamsState) != kDialogContinues)
  353.         goto ALL_DONE ;
  354.     
  355.     /* Perfrom the Logoff state */
  356.  
  357.     if (PerformAState(dlg, kTest4LogoffState) != kDialogContinues)
  358.         goto ALL_DONE ;
  359.  
  360.     /* Perfrom the disconnect state */
  361.  
  362.     if (PerformAState(dlg, kTest4DisconnectState) != kDialogContinues)
  363.         goto ALL_DONE ;
  364.     
  365.     /* Perfrom the cleanup state */
  366.  
  367.     if (PerformAState(dlg, kTest4CleanupState) != kDialogContinues)
  368.         goto ALL_DONE ;
  369.  
  370. ALL_DONE:
  371.     dlg->EndModal() ;
  372.     delete dlg ;
  373. }
  374.